package com.isesol.ime.validator.message;

import com.isesol.arch.fluentvalidator.*;
import com.isesol.ime.dal.mapper.*;
import com.isesol.ime.dal.model.*;
import com.isesol.ime.dal.model.dto.*;
import com.isesol.ime.enums.*;
import com.isesol.ime.service.*;
import org.apache.commons.collections.*;
import org.springframework.stereotype.*;

import javax.annotation.*;

@Component
public class MessageDelValidator extends ValidatorHandler<MessageDTO> implements Validator<MessageDTO> {

    @Resource
    MessageMapper messageMapper;

    @Resource
    MessageService messageService;

    @Override
    public boolean validate(ValidatorContext context, MessageDTO messageDTO) {

        boolean result = true;

        MessageDO message = messageMapper.getById(messageDTO.getId());

        if (null == message) {

            context.addErrorMsg(ErrorCode.MESSAGE_NOT_EXISTS.getMessage());
            result = false;
        }

        if (!CollectionUtils.isEmpty(messageService.getSubs(messageDTO.getId()))){

            context.addErrorMsg(ErrorCode.MachineToolMessage_IN_USE.getMessage());
            result = false;
        }

        return result;
    }


}